import requests
import json
import base64
from pathlib import Path
import time
import pandas as pd

#We use the OpenRoute API for model evaluation. Please feel free to substitute it with any service you prefer.

def encode_image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

url = "https://openrouter.ai/api/v1/chat/completions"
headers = {
    "Authorization": f"Bearer xxxxxxxxxxxxxx",# Here is your key.
    "Content-Type": "application/json"
}

def load_questions_and_pictures_from_csv(csv_file_path):#We need to get our question dataset here. Get text questions and  corresponing pictures.
    df = pd.read_csv(csv_file_path)
    questions = df['Question'].tolist()
    pictures = df['Picture_ID'].tolist()
    return questions, pictures

csv_file_path = "D:\\accessability\\ACCESSIBILITY_RESULTS\\English_Dataset_Referent_Identification_With_Accessibility.csv"
questions, pictures = load_questions_and_pictures_from_csv(csv_file_path)
for model_name in ["openai/gpt-4.1", "qwen/qwen2.5-vl-32b-instruct:free","meta-llama/llama-4-maverick:free"]:#Here we put three models. You can add more, or change it.
    for run_number in range(1, 11):  # Run 10 times.
        results_df = pd.DataFrame(columns=['Question', 'Answer'])
        count = 0
        while count < len(questions):
            try:
                for i in range(count, len(questions)):
                    question = questions[i]
                    picture = pictures[i]
                    file_path = f"D:\\accessability\\ACCESSIBILITY_RESULTS\\pictures\\{picture}.png"
                    base64_image = encode_image_to_base64(file_path)
                    data_url = f"data:image/jpeg;base64,{base64_image}"
                    messages = [
                        {
                            "role": "user",
                            "content": [
                                {
                                    "type": "text",
                                    "text": question
                                },
                                {
                                    "type": "image_url",
                                    "image_url": {
                                        "url": data_url
                                    }
                                }
                            ]
                        }
                    ]
                    payload = {
                        "model": model_name,
                        "messages": messages
                    }
                    response = requests.post(url, headers=headers, json=payload)
                    print(f"Question: {question}")
                    data = response.json()
                    answer = data["choices"][0]["message"]["content"]
                    print(answer)
                    new_row = pd.DataFrame({'Question': [question], 'Answer': [answer]})
                    results_df = pd.concat([results_df, new_row], ignore_index=True)
                    count += 1  # Update the counter to proceed to the next question.
            except Exception as e:
                print(f"Error at count {count}: {e}")
                # Save temporary results
                results_df.to_csv(f"D:\\accessability\\{model_name[0:2]}_temporary_update_questions_answers_run_{run_number}_{count}.csv", index=False, encoding='utf-8-sig')
                # Continue processing the remaining parts after catching the error.

        # Save the final results to a new CSV file.
        results_df.to_csv(f"D:\\accessability\\{model_name[0:2]}_update_questions_answers_run_{run_number}.csv", index=False, encoding='utf-8-sig')
